package zjc.bookkeeping.fragment.statistics;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.progressbar.BGAProgressBar;
import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.tools.ToolsClass;

public class WeekClassListAdapter extends BaseAdapter {
    private Context context;
    private float sum;
    //List<String> classes_name = DBManager.getAllConsumptionClass();
    private List<WeekClassListData> weekClassListData = new ArrayList<>();
    String[] colors = {"#eea2a4","#c08eaf","#8076a3","#93b5cf","#1ba784","#fbda41"};

    @Override
    public int getCount() {
        return weekClassListData.size();
    }

    @Override
    public Object getItem(int i) {
        return weekClassListData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public Context getContext() {
        return context;
    }

    public WeekClassListAdapter(Context context, float sum, List<WeekClassListData> data) {
        this.context = context;
        this.sum = sum;
        this.weekClassListData = data;
    }

    @SuppressLint("ResourceType")
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LinearLayout ll = (LinearLayout) LayoutInflater.from(this.getContext()).inflate(R.layout.class_list_item, null);
        TextView class_name = ll.findViewById(R.id.class_name);
        BGAProgressBar progressBar = ll.findViewById(R.id.class_progress);
        TextView class_money = ll.findViewById(R.id.class_money);
        ImageView class_img = ll.findViewById(R.id.class_img);
        TextView class_sum = ll.findViewById(R.id.class_sum);
        class_sum.setText("消费" + weekClassListData.get(i).getClass_sum() + "笔");
        String imageName = DBManager.getConsumptionClassImage(weekClassListData.get(i).getClass_name());
        class_img.setImageResource(ToolsClass.getResourceId(imageName));
        class_name.setText(weekClassListData.get(i).class_name);
        class_money.setText(String.valueOf(weekClassListData.get(i).getClass_consumption()));
        progressBar.setMax((int) 100);
        int j = i;
        while (j >= 6){
            j -= 6;
        }
        //Paint mPaint = new Paint();
       // mPaint.setColor(Color.parseColor(colors[i]));
        //progressBar.setProgressDrawable(ll.findViewById(R.drawable.progressbar));
        //progressBar.
       // Log.i("分类", String.valueOf((int) getClassSumConsumption(weekClassListData.get(i).class_name)));
        progressBar.setProgress(DataProcess(weekClassListData.get(i).getClass_consumption()));
        return ll;
    }

    public int DataProcess(float data){
        int temp = Math.round(data * 100 / sum);
        Log.i("分类", String.valueOf(temp));
        return  temp;
    }

}
